home *** CD-ROM | disk | FTP | other *** search
- /*
- * Get a string with editing functions by Paul Roub
- *
- * This is somewhat off the top of my head, but it should work. It
- * assumes that backspace is not destructive on your display
- * (although we'll MAKE it destructive when editing), and that
- * Control-G sounds a bell (which will be the 'message' you asked
- * for). Input is terminated by a CR. It also assumes that you
- * have the function getch() (or a work-alike, which gets a
- * character from the keyboard WITHOUT echo.
- *
- */
-
- #include <conio.h> /* MSC needs this for getch() */
- #include <ctype.h>
- #include <stdio.h>
-
-
- #define BELL 7 /* Ctrl-G */
- #define BS 8 /* backspace (Ctrl-H) */
- #define CR 13 /* Carriage return */
-
- void GetStr(char *st, int maxlen)
- {
- int len = 0, ch;
-
- putchar('['); /* display our 'field' */
- for (len = 0; len < maxlen; len++)
- putchar(' ');
- putchar(']');
-
- putchar(BS); /* and get into position */
- for (len = maxlen; len > 0; len--)
- putchar(BS);
-
- len = 0;
-
- while ((ch = getch()) != CR)
- {
- if (ch == BS) /* backspace */
- {
- if (len == 0) /* already at beginning? */
- putchar(BELL); /* yes, complain */
- else
- { /* no, backup and space */
- len--;
- st--;
- putchar(BS);
- putchar(' ');
- putchar(BS);
- }
- }
- else if (iscntrl(ch) || (len == maxlen)) /* illegal or */
- putchar(BELL); /* too long */
- else
- { /* useable character */
- *st++ = (char)ch; /* put it in string */
- len++; /* note another character */
- putchar(ch); /* and display */
- }
- }
- *st = 0; /* terminate the string */
- return;
- }
-